package com.itheima.health.service.impl;

import com.alibaba.dubbo.config.annotation.Service;
import com.itheima.health.dao.ExaminationReportDao;
import com.itheima.health.dao.OrderDao;
import com.itheima.health.pojo.CheckGroup;
import com.itheima.health.pojo.CheckItem;
import com.itheima.health.pojo.ExaminationReport;
import com.itheima.health.pojo.Setmeal;
import com.itheima.health.service.CheckResultService;
import com.itheima.health.utils.QiNiuUtils;
import com.itheima.health.utils.SMSUtils;
import com.itheima.health.utils.mailUtils;
import org.apache.poi.ss.usermodel.Sheet;
import org.apache.poi.ss.usermodel.Workbook;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
import org.springframework.beans.factory.annotation.Autowired;

import java.io.*;
import java.net.MalformedURLException;
import java.net.URL;
import java.util.List;

/**
 * @Author ： ZAG WELL
 * @Date : 2020/12/7
 */
@Service(interfaceClass = CheckResultService.class)
public class CheckResultServiceImpl implements CheckResultService {

    @Autowired
    private OrderDao orderDao;

    @Autowired
    private ExaminationReportDao examinationReportDao;

//    @Autowired
//    private FileDao fileDao;


    @Override
    public void updateAdvice(ExaminationReport examinationReport) {
        System.out.println("examinationReport.getId() = " + examinationReport.getId());
        System.out.println("examinationReport.getSetmeal() = " + examinationReport.getSetmeal());
        //非空判断
        if (examinationReport!=null) {
//            获取每个checkgroup对象
            for (CheckGroup checkGroup : examinationReport.getCheckGroups()) {
//                获取每个checkItem对象
                for (CheckItem checkItem : checkGroup.getCheckItems()) {
//                    遍历检查项 一个一个更新检查项信息
                    examinationReportDao.updateExaminationReport(checkItem.getAdvice(), examinationReport.getId(), checkItem.getId());
                }
            }
        }
    }

    @Override
    public void generatAdvice(Integer orderId) {
        List<Integer> checkItemIds = orderDao.getCheckItemIdsByOrderId(orderId);
        if (checkItemIds.size() != 0) {
            for (Integer checkItemId : checkItemIds) {
                orderDao.generatAdvice(orderId, checkItemId);
            }
        }
    }

    @Override
    public ExaminationReport getExaminationReport(Integer id) {
        System.out.println(id);
        return examinationReportDao.getExaminationReport(id);
    }

    @Override
    public void uplodedAndSendEmail(ExaminationReport examinationReport,String username) throws IOException {
        System.out.println("examinationReport!=null = " + examinationReport != null);
        System.out.println("examinationReport.getMember() = " + examinationReport.getMember());
        String template_report = "http://qkaru3r44.hn-bkt.clouddn.com/%E4%BC%A0%E6%99%BA%E5%81%A5%E5%BA%B7%E4%BD%93%E6%A3%80%E6%8A%A5%E5%91%8A20201207.xlsx";
        InputStream workBookInputStream = new URL(template_report).openStream();
        Workbook wk = new XSSFWorkbook(workBookInputStream);
        Sheet sheetAt = wk.getSheetAt(0);
//        报表头信息 姓名、性别、检查日期、身份证
//        姓名
        sheetAt.getRow(1).getCell(1).setCellValue(examinationReport.getMember());
        String sex = null;
        if ("1".equals(examinationReport.getSex())) {
            sex = "男";
        } else {
            sex = "女";
        }
//        性别
        sheetAt.getRow(1).getCell(5).setCellValue(sex);
//        体检日期
        sheetAt.getRow(2).getCell(1).setCellValue(examinationReport.getOrderDate());
//        身份证号码
        String idCard = examinationReport.getIdCard();
        idCard = idCard.replace(idCard.substring(6, 13), "*****");
        sheetAt.getRow(2).getCell(5).setCellValue(idCard);

//        ----------------------------------------------------------------

        int row = 5;
        int cell = 0;
        List<CheckGroup> checkGroups = examinationReport.getCheckGroups();
        for (CheckGroup checkGroup : checkGroups) {
            for (CheckItem checkItem : checkGroup.getCheckItems()) {
                sheetAt.getRow(row).getCell(cell).setCellValue(checkItem.getName());
                sheetAt.getRow(row).getCell(cell+2).setCellValue(checkItem.getAdvice());
                sheetAt.getRow(row).getCell(cell+6).setCellValue(username);
                row++;
                cell = 0;
            }
        }
        System.out.println(wk);
//        wk.write(new FileOutputStream(new File("C:\\Users\\ZAG WELL\\Desktop\\"+examinationReport.getMember()+"传智健康体检报告.xlsx")));
        ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
        wk.write(outputStream);
        wk.close();
//        获取字节数据流 把excel文件转换成二进制 上传七牛云
        byte[] bytes = outputStream.toByteArray();
        String file = examinationReport.getMember() + "_" + examinationReport.getOrderDate() + "_medical_examination_report.xlsx";
        try {
            QiNiuUtils.uploadViaByte(bytes, file);
        } catch (Exception e) {
            e.printStackTrace();
            System.out.println("上传失败");
        }
//        发邮件
//        拼接下载路径
        String downLink = "http://qkaru3r44.hn-bkt.clouddn.com/" + file;
        mailUtils.sendMail("451969238@qq.com", "尊敬的用户，你的体检报告已生成请前往以下路径下载" + downLink, "体检报告");

//        把信息存入数据库 供后面定时删除
    }

}
